package com.genius.adminmanager.menu.controller;

import java.sql.SQLException;

import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.menu.entity.MenuJson;
import com.genius.adminmanager.menu.service.MenuUtils;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.adminmanager.other.entity.TreeJson;
import com.genius.workflow.page.PageResult;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.TreeUtils;

/**
* @Author: woy
* @Description: 菜单查询
* @Date: Created in 2020/2/3 19:28
* @param 
*/
@Controller
public class MenuSelectController {

	/**
	 * 跳转到菜单列表页面
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("5e68cc3e00ae426da7a7e8fcd07b1389")
	@GetMapping("/adminmanager/menu/toMenuList")
	public String toMenuList(){
		return "/views/adminmanager/menu/select";
	}


	/**
	 * 查看菜单列表
	 * 根据传入的id查看该页面显示的数据是否有权限
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/adminmanager/menu/menuListHead")
	@ResponseBody
    public JSONObject menuListHead(@RequestBody JSONObject json,HttpSession session){
    	JSONObject response = new JSONObject();
    	response.put(ResponseJson.code, 0);
    	String menuId = json.getString(MenuJson.menuId);
    	
    	String roleId = (String)session.getAttribute(SessionJson.roleId);
		if(roleId == null){
    		response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_Session_Failure);
    		return response;
    	}
    	
		response = MenuUtils.getPowerFileByMenu(roleId,menuId, response);
    	JSONObject result =  response.getJSONObject(ResponseJson.data);
    	if(result ==null){
    		response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
    		return response;
    	}
    	return response;
    }
	/**
	 * 查询数据
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/adminmanager/menu/menuListBody")
	@ResponseBody
    public JSONObject menuListBody(@RequestBody JSONObject json){
    	JSONObject response = new JSONObject();
    	response.put(ResponseJson.code, 0);
    	
    	String table = "t_menu";
    	String headField = json.getString(PageResult.headField);
    	String idValue = json.getString(TreeJson.pid);
    	String where = "";
    	String orderBy = " order by "+MenuJson.place;
    	try {
        	JSONArray arr = TreeUtils.getOnceTree(MenuJson.menuId, idValue, headField,table,MenuJson.resource, where, orderBy);
        	response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.data, arr);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, "查询菜单出错");
		}
    	return response;
    }
}
